//
//  MySettingVC.m
//  JinFuThree
//
//  Created by 毛韶谦 on 2018/9/1.
//  Copyright © 2018年 毛韶谦. All rights reserved.
//

#import "MySettingVC.h"
#import "ImportSelectView.h"
#import "CleanCaches.h"
#import "SettingAboutVC.h"

@interface MySettingVC ()

@property (nonatomic, strong) UIScrollView *mainScrollView;

@property (nonatomic, strong) ImportSelectView *clearView;
@property (nonatomic, strong) ImportSelectView *aboutView;

@property (nonatomic, strong) UIButton *logOutBtn;

@end

@implementation MySettingVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    [self initUI];
    // Do any additional setup after loading the view.
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    self.clearView.rightLabel.text = [NSString stringWithFormat:@"%.2fM",[CleanCaches sizeWithFilePath:[QuickDataLocalStorageTool libraryPath]]+[CleanCaches sizeWithFilePath:[QuickDataLocalStorageTool tmpPath]]];
}



- (void)initUI {
    
    self.title = @"设置";
    
    self.mainScrollView.backgroundColor = ListMaoBackgroundColor;
    
    [self.clearView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.top.mas_equalTo(self.mainScrollView.mas_top);
        make.height.mas_equalTo(55);
    }];
    [self.aboutView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.mas_equalTo(self.view);
        make.top.mas_equalTo(self.clearView.mas_bottom);
        make.height.mas_equalTo(55);
    }];
    
    [self.logOutBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(self.view.mas_left);
        make.right.mas_equalTo(self.view.mas_right);
        make.top.mas_equalTo(self.aboutView.mas_bottom).offset(25);
        make.height.mas_equalTo(45);
    }];
}

- (void)clickAction:(UIButton *)sender {
    
    if (sender.tag == 1) {
        [self cleanLoadData];
    }else {
        SettingAboutVC *vc = [[SettingAboutVC alloc] init];
        [self.navigationController pushViewController:vc animated:YES];
    }
}

- (void)logOutBtnAction {
    
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"退出登录" message:@"是否退出登录？" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"退出登录" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        if ([[Mediator sharedInstance] Login_LogOutAction]) {
            [HUD_Mao showSuccessMessage:@"退出登录成功"];
            [self.navigationController popToRootViewControllerAnimated:YES];
        }else {
            [HUD_Mao showFailMessage:@"退出登录失败"];
        }
    }];
    UIAlertAction *alertCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [alertC addAction:alertAction];
    [alertC addAction:alertCancel];
    [self presentViewController:alertC animated:YES completion:nil];
}

- (void)cleanLoadData {
    
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:@"清空缓存" message:@"确定清空内容，释放更多手机空间" preferredStyle:UIAlertControllerStyleAlert];
    UIAlertAction *alertAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        
        CGFloat cachesMuch = [CleanCaches sizeWithFilePath:[QuickDataLocalStorageTool libraryPath]]+[CleanCaches sizeWithFilePath:[QuickDataLocalStorageTool tmpPath]];
        
        if (cachesMuch > 0.0f) {
            MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
            //            hud.delegate = self;
            //加载条上显示文本
            hud.label.text = [NSString stringWithFormat:@"急速清理中(共 %.2f M)",cachesMuch];
            //设置对话框样式
            hud.mode = MBProgressHUDModeAnnularDeterminate;
            
            dispatch_async(dispatch_get_global_queue(QOS_CLASS_USER_INITIATED, 0), ^{
                float progress = 0.0f;
                while (progress < 1.0f) {
                    progress += 0.01f;
                    
                    dispatch_async(dispatch_get_main_queue(), ^{
                        [MBProgressHUD HUDForView:self.view].progress = progress;
                    });
                    [NSThread sleepForTimeInterval:0.01];
                }
                
                dispatch_async(dispatch_get_main_queue(), ^{
                    [hud hideAnimated:YES];
                    //清除caches文件下所有文件
                    [CleanCaches clearSubfilesWithFilePath:[QuickDataLocalStorageTool libraryPath]];
                    [CleanCaches clearSubfilesWithFilePath:[QuickDataLocalStorageTool tmpPath]];
                    self.clearView.rightLabel.text = @"0.0MB";
                });
            });
        }else {
            
            [HUD_Mao showCharacterMessage:@"缓存清理干净"];
        }
        
    }];
    UIAlertAction *alertCancel = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    
    [alertC addAction:alertAction];
    [alertC addAction:alertCancel];
    [self presentViewController:alertC animated:YES completion:nil];
    
    
}
- (UIScrollView *)mainScrollView {
    
    if (!_mainScrollView) {
        _mainScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidthMao, ScreenHeight - SafeAreaTopHeight)];
        _mainScrollView.contentSize = CGSizeMake(ScreenWidthMao, 450);
        _mainScrollView.showsVerticalScrollIndicator = NO;
        _mainScrollView.showsHorizontalScrollIndicator = NO;
        _mainScrollView.bounces = NO;
        
        [self.view addSubview:_mainScrollView];
    }
    return _mainScrollView;
}

- (ImportSelectView *)clearView {
    
    if (!_clearView) {
        _clearView = [[ImportSelectView alloc] init];
        _clearView.titleLabel.text = @"清除缓存";
        _clearView.rightLabel.textColor = HexColor(0x999999);
        [self.mainScrollView addSubview:_clearView];
        
        UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        leftBtn.tag = 1;
        [leftBtn addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
        [_clearView addSubview:leftBtn];
        [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.top.bottom.mas_equalTo(_clearView);
            make.left.mas_equalTo(_clearView.titleLabel.mas_right);
        }];
    }
    return _clearView;
}
- (ImportSelectView *)aboutView {
    
    if (!_aboutView) {
        _aboutView = [[ImportSelectView alloc] init];
        _aboutView.titleLabel.text = @"关于";
        _aboutView.lineImageView.hidden = YES;
        _aboutView.rightLabel.text = @"";
        
        [self.mainScrollView addSubview:_aboutView];
        
        UIButton *leftBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        leftBtn.tag = 2;
        [leftBtn addTarget:self action:@selector(clickAction:) forControlEvents:UIControlEventTouchUpInside];
        [_aboutView addSubview:leftBtn];
        [leftBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.top.bottom.mas_equalTo(_aboutView);
            make.left.mas_equalTo(_aboutView.titleLabel.mas_right);
        }];
    }
    return _aboutView;
}

- (UIButton *)logOutBtn {
    
    if (!_logOutBtn) {
        _logOutBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        _logOutBtn.backgroundColor = [UIColor whiteColor];
        [_logOutBtn setTitle:@"退出登录" forState:UIControlStateNormal];
        [_logOutBtn setTitleColor:ThemeMaoColor forState:UIControlStateNormal];
        _logOutBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [_logOutBtn addTarget:self action:@selector(logOutBtnAction) forControlEvents:UIControlEventTouchUpInside];
        
        [self.view addSubview:_logOutBtn];
    }
    return _logOutBtn;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
